package com.lz.ovuola.util.fastdfs;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import com.lz.ovuola.util.exception.AppStartupException;

/**
 * 加载fdfs_client.conf
 * 
 * @author fz
 *
 */
@Configuration
@ConfigurationProperties(prefix = "fdfs.client.conf")
public class DfsConfig implements CommandLineRunner {
	private static String path;
	private static String ip;
	private static String download;
	private static boolean testEnv;

	private static TrackerClient tc;
	private static TrackerServer ts;

	private static Logger logger = LoggerFactory.getLogger("DfsConfig");

	@Override
	public void run(String... args) throws AppStartupException {
		String fullPath = "";
		try {
			if (testEnv) {
				path = "classpath:fdfs_client.conf";
				ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
				fullPath = resourcePatternResolver.getResource(path).getFile()
						.getAbsolutePath();
				// fullPath = ResourceUtils.getFile(path).getAbsolutePath();
			} else {
				// 由于jar不支持加载classpath文件下绝对路径 所以只能写绝对路径了......
				fullPath = path;
			}
			// logger.info("---path :{} fullPath：{}", path, fullPath);
			if (StringUtils.isEmpty(fullPath))
				throw new AppStartupException(
						"fastdfs fullPath is null or \"\" ");
			ClientGlobal.init(fullPath);
			tc = new TrackerClient(ClientGlobal.g_tracker_group);
			ts = tc.getConnection();
			if (ts == null) {
				logger.error("TrackerServer is null");
				throw new AppStartupException("TrackerServer is null");
			}
			download = new StringBuilder(ip).append(":")
					.append(ClientGlobal.getG_tracker_http_port()).append("/")
					.toString();
			logger.info("---fastdfs startup success fullPath: {}", fullPath);
		} catch (IOException e) {
			logger.error("---IOException msg:{}", e.getMessage());
			throw new AppStartupException("fastdfs fullPath is null or \"\" ");
		} catch (MyException e) {
			logger.error("---MyException msg:{}", e.getMessage());
			throw new AppStartupException("fastdfs fdfs_client.conf is wrong");
		}
	}

	public static boolean isTestEnv() {
		return testEnv;
	}

	public static void setTestEnv(boolean testEnv) {
		DfsConfig.testEnv = testEnv;
	}

	public static String getPath() {
		return path;
	}

	public static void setPath(String path) {
		DfsConfig.path = path;
	}

	public static String getIp() {
		return ip;
	}

	public static void setIp(String ip) {
		DfsConfig.ip = ip;
	}

	public static TrackerClient getTc() {
		return tc;
	}

	public static TrackerServer getTs() {
		return ts;
	}

	public static String getDownload() {
		return download;
	}

}
